

const {ccclass, property} = cc._decorator;

import { setExpressionMap, expressionMap } from './dataSet'
import dialogControl from './dialogControl';
import paojieControl from './paojieControl';

class Message {
    name: string;
    content: string;
    face: string;
    mouth: string;

    constructor(name: string, content: string, face: string, mouth: string) {
        this.name = name;
        this.content = content;
        this.face = face;
        this.mouth = mouth;
    }
}

class Person {
    name: string ;
    age: number
    constructor() {
        
    }
}

@ccclass
export default class CanvasControl extends cc.Component {
    // 人物和消息控制器
    @property(paojieControl)
    paojieControl: paojieControl = null;
    @property(dialogControl)
    dialogControl: dialogControl = null;

    // 消息数组
    msgs: Message[] = null;
    // 当前是第几条消息
    index: number = 0

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    // 时间
    textTime: number = 0; 

    start () {
        this.msgs = [
            new Message('炮姐', '......', 'paojieface_01', 'paojiemouth_01'),
            new Message('炮姐', '今天天气不错', 'paojieface_01', 'paojiemouth_02'),
            new Message('炮姐', '不开心了', 'paojieface_02', 'paojiemouth_01'),
            new Message('炮姐', '还行吧', 'paojieface_02', 'paojiemouth_02'),
        ]

        // this.playMsg(0)
        this.dialogControl.node.active = false;  // 让对话框先隐藏
        // 鼠标点击对话
        this.node.on(cc.Node.EventType.TOUCH_START, ()=>{
            if (this.index >= this.msgs.length) {
                this.dialogControl.node.active = false;
                this.playMsg(0);
                // 获取数据
                let data = JSON.parse(cc.sys.localStorage.getItem('878989566'))
                console.log(data);
            } else {
                this.dialogControl.node.active = true;
                this.playMsg(this.index);
                this.index++;
            }
        })

        this.saveData('878989566', this.msgs);
        let person1 = new Person();
        person1.name = '李四';
        person1.age = 123;

        this.saveData('234234', person1);
        let person2: Person = Object.assign(new Person(), JSON.parse(cc.sys.localStorage.getItem('234234')))
        console.log(person2);
        
    }

    // 存储数据
    saveData(key: any, value: any) {
        cc.sys.localStorage.setItem(key, JSON.stringify(value))
    }

    // 表情和话语
    playMsg(index: number) {
        
        this.paojieControl.setExpression(this.msgs[index].face, this.msgs[index].mouth);
         // 设置人名
         this.dialogControl.getComponent(dialogControl).setPerson(this.msgs[index].name);
         // 设置话语
         this.dialogControl.getComponent(dialogControl).setMessage(this.msgs[index].content)
    }


    update (dt) {
        // 随时间出现对话
        // if (this.index >= this.msgs.length ) return;
        // this.textTime += dt;
        // if (this.textTime >= 1) {
        //     this.playMsg(this.index);
        //     this.index++;
        //     this.textTime = 0;
        // }

    }
}
